using System.Collections.Generic;
using System.Linq;

namespace Magiq.Update {
    public class QueryableUpdate<T> : BaseUpdate<T>, IQueryableUpdate<T> where T : class {
        private readonly IUpdateProvider provider;

        public QueryableUpdate(IQueryable<T> queryable) {
            Where = queryable;
            provider = Magiq.For(queryable).GetUpdateProvider();
        }

        #region IQueryableUpdate<T> Members

        public override int Update() {
            return PropertySets.Count == 0
                       ? Where.Count()
                       : provider.Execute(this);
        }

        IEnumerable<IPropertySet> IQueryableUpdate<T>.PropertySets {
            get { return PropertySets; }
        }

        public IQueryable<T> Where { get; private set; }

        #endregion
    }
}